using System.IO;
using Microsoft.Data.Sqlite;

namespace IMClient.Cache
{
    public sealed class DbContext : IDisposable
    {
        private static readonly string AppDataPath = Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
            "IMClient"
        );

        private static readonly DbContext _instance = new DbContext();
        private readonly SqliteConnection _connection;


        // 私有构造函数，防止外部实例化
        private DbContext()
        {
            // 确保应用程序数据目录存在
            if (!Directory.Exists(AppDataPath))
            {
                Directory.CreateDirectory(AppDataPath);
            }

            string dbPath = Path.Combine(AppDataPath, "imclient.db");
            string connectionString = $"Data Source={dbPath}";

            _connection = new SqliteConnection(connectionString);
            _connection.Open();
            CreateTables();
        }
        
        public static DbContext Instance => _instance;

        private void CreateTables()
        {
            var command = _connection.CreateCommand();
            command.CommandText = @"
                CREATE TABLE IF NOT EXISTS friend_cache (
                    user_id TEXT NOT NULL,
                    friend_id TEXT NOT NULL,
                    friend_name TEXT NOT NULL,
                    friend_avatar TEXT,
                    last_update_time TEXT NOT NULL,
                    PRIMARY KEY (user_id, friend_id)
                );

                CREATE TABLE IF NOT EXISTS chat_history(
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    chat_type TEXT NOT NULL,      
                    content_type TEXT NOT NULL,  
                    sender_id TEXT NOT NULL,    
                    sender_name TEXT NOT NULL,   
                    sender_avatar TEXT,        
                    receiver_id TEXT NOT NULL,   
                    content TEXT NOT NULL,      
                    is_read INTEGER NOT NULL,    
                    is_sent_by_user INTEGER NOT NULL, 
                    timestamp TEXT NOT NULL  
                );

                CREATE TABLE IF NOT EXISTS chat_records (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    friend_id TEXT NOT NULL,
                    friend_name TEXT NOT NULL,
                    friend_avatar TEXT,
                    last_message TEXT,
                    last_message_time TEXT,
                    user_id TEXT NOT NULL,
                    type TEXT NOT NULL DEFAULT 'friend',
                    UNIQUE (friend_id, user_id)
                );

                CREATE TABLE IF NOT EXISTS group_cache (
                    user_id TEXT NOT NULL,
                    group_id TEXT NOT NULL,
                    group_name TEXT NOT NULL,
                    group_avatar TEXT,
                    creator TEXT NOT NULL,
                    last_update_time TEXT NOT NULL,
                    PRIMARY KEY (user_id, group_id)
                );

            CREATE TABLE IF NOT EXISTS group_chat_history (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    user_id TEXT NOT NULL,     
                    group_id TEXT NOT NULL,    
                    chat_type TEXT NOT NULL,     
                    content_type TEXT NOT NULL, 
                    sender_id TEXT NOT NULL,
                    sender_name TEXT NOT NULL,
                    sender_avatar TEXT,
                    group_avatar TEXT,
                    content TEXT NOT NULL,
                    is_read INTEGER NOT NULL,
                    timestamp TEXT NOT NULL,
                    is_sent_by_user INTEGER NOT NULL
                );

            ";
            command.ExecuteNonQuery();
        }

        public SqliteConnection GetConnection()
        {
            return _connection;
        }

        public void Dispose()
        {
            _connection?.Dispose();
        }
    }
}